<?php
namespace app\api\logic;
use think\Db;
class Course extends Base{

    private $AccountInfo = [];

    public function __construct()
    {
        parent::__construct();
    }

    // 课程信息
    public function cinfo($param)
    {   
        $cid = isset($param['cid'])?intval($param['cid']):0;
        // 课程信息
        $where[] =['c.id','=',$cid];
        $course=Db::name('course')->alias('c')->where($where)->find();
        if (!$course){
            $this->return_data['Msg'] = "无此课程";
            return $this->return_data;
        }
        $course['description'] = str_replace('<img', '<img width="100%"', $course['description']);
        // $course['begin_time'] = date('H:i',$course['begin_time']);
        // $course['end_time'] = date('H:i',$course['end_time']);
        $course['classes_name'] = Db::name('store')->where(['id'=>$course['sid']])->value('name').$course['classes_name'];
        $course['cover'] = get_newpic_url($course['pic']);
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = ['course'=>$course];
        return $this->return_data;
    }

    // 课时对应的课程信息
    public function classtimecourseinfo($param)
    {
        $cid = isset($param['cid'])?intval($param['cid']):0;
        $where[] =['ct.id','=',$cid];
        $course=Db::name('classtime')->alias('ct')->leftJoin('course c','c.id=ct.course_id')->field('c.*,ct.begin_time,ct.end_time')->where($where)->find();
        if (!$course){
            $this->return_data['Msg'] = "无此课程";
            return $this->return_data;
        }
        $course['description'] = str_replace('<img', '<img width="100%"', $course['description']);
        $course['begin_time'] = date('H:i',$course['begin_time']);
        $course['end_time'] = date('H:i',$course['end_time']);
        $course['classes_name'] = Db::name('store')->where(['id'=>$course['sid']])->value('name').$course['classes_name'];
        $course['cover'] = get_newpic_url($course['pic']);
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = ['course'=>$course];
        return $this->return_data;
    }

    // 课程信息
    public function my_course_info($param)
    {   
        $cid = isset($param['cid'])?intval($param['cid']):0;
        $uid = isset($param['user_id']) ? $param['user_id']:0;
        $user = Db::name('user')->where('id',$uid)->find();
        if (!$user) {
            $this->return_data['Msg'] = "未查询到该用户";
            return $this->return_data;
        }
        $where[] =['c.id','=',$cid];
        $course=Db::name('course')->alias('c')->where($where)->find();
        if (!$course){
            $this->return_data['Msg'] = "无此课程";
            return $this->return_data;
        }
        $user_course = Db::name('user_course')->where(['user_id'=>$uid,'course_id'=>$cid])->find();
        if (!$user_course) {
            $this->return_data['Msg'] = "未查询到该课程";
            return $this->return_data;
        }
        $course['description'] = str_replace('<img', '<img width="100%"', $course['description']);
        // $course['begin_time'] = date('H:i',$course['begin_time']);
        // $course['end_time'] = date('H:i',$course['end_time']);
        $course['classes_name'] = Db::name('store')->where(['id'=>$course['sid']])->value('name').$course['classes_name'];
        $course['cover'] = get_newpic_url($course['pic']);
        $course['classtime_sum'] = $user_course['classtime_sum'];
        $course['classtime_surplus'] = $user_course['classtime_surplus'];
        $course['entrance_time'] = date('Y-m-d',$user_course['createtime']);
        $course['end_time'] = intval(Db::name('classtime')->where(['course_id'=>$cid])->order('no DESC')->value('end_time'));
        $course['end_time'] = date('Y-m-d',$course['end_time']);
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = ['course'=>$course];
        return $this->return_data;
    }
}
